$hw={
	Clear-Host
	"Hello world"
}

& $hw

# Functions are basically script blocks with name.
function Write-HelloWorld(){
	Clear-Host
	"Hello world"
}

Write-HelloWorld

# When writing functions, use an approved verb
Get-Verb

# Parameters can be passed in by placing them in parenthesis
function Get-Fullname($firstName,$lastName)
{
	Write-Host ($firstName+" "+$lastName)
}

# Calling the function
Get-Fullname "Arcane" "Code"

$myVar="Arcane"

Get-Fullname $myVar "Code"
Get-Fullname $("Ar"+"cane") "Code"

# Any changes to paramenter inside a function are scoped to that function
function Set-NonRefVar($myparam)
{
	$myparam=33
	"Inside function `$myparam=$myparam"
}

Clear-Host
$myparam=42
"Prior to function `$myparam=$myparam"
Set-NonRefVar $myparam
"After function `$myparam=$myparam"

# To change a value inside a function, use [ref]
function Set-RefVar([ref] $myparam)
{
	$myparam.GetType()
	$myparam=33
	"Inside function `$myparam=$($myparam.Value)"
}
Clear-Host
$myparam=42
"Prior to function `$myparam=$myparam"
Set-RefVar ([ref] $myparam)
"After function `$myparam=$myparam"

# It is possible to pipeline enable your functions
# These are referred to as advanced functions
function Get-PSFiles()
{
	# The begin block executes once at the start of the function
	begin {$retval="Here are some PowerShell files:`r`n"}

	# The process block is executed once for each object being
	# passed in from the pipe
	process{
		if($_.Name -like "*.ps1")
		{
			$_.Name
			$retval += "`t$($_.Name)`r`n"
		}
	}

	# The end block executes once, after the rest of the function
	end {return $retval}
}

Clear-Host
Set-Location C:\code\LearningPowerShell\Grammars\BeginningScripts
Get-ChildItem | Get-PSFiles

$output=Get-ChildItem | Get-PSFiles
$output.GetType()

Clear-Host
$i=0
foreach($f in $output)
{
	$i++
	"$i : $f"
}

# To pipeline the output, push the output in the process area
function Get-PSFiles()
{
	begin{}

	process{
		if($_.Name -like "*.ps1")
		{
			$retval="`tPowerShell file is $($_.Name)"
			$retval # This is the equivalent of:return $retval
		}
	}

	end{}
}

$output=Get-ChildItem | Get-PSFiles
$output.GetType()

Clear-Host
$i=0
foreach($f in $output)
{
	$i++
	"$i : $f"
}

function Write-SomeText()
{
	# begin{}

	process{
		$retval="Here is the output: $($_)"
		$retval
	}

	# end{}
}

Clear-Host
Set-Location "C:\code\LearningPowerShell\Grammars\BeginningScripts"
Get-ChildItem | Get-PSFiles | Write-SomeText

